<template>
  <div class="top-list">
    <music-list :bg-image="bgImage" :title="title" :songs="songs" :rank="rank"/>
  </div>
</template>

<script type="text/ecmascript-6">
import MusicList from 'components/music-list/music-list'
import { mapGetters } from 'vuex'
import { getMusicList } from 'api/rank'
import { ERR_OK } from 'api/config'
// import { getSongVkey } from 'api/song'
import { createSong, isValidMusic, processSongsUrl } from 'common/js/song'
export default {
  data () {
    return {
      songs: [],
      rank: true
    }
  },
  computed: {
    title () {
      return this.toplist.topTitle
    },
    bgImage () {
      return this.toplist.picUrl
    },
    ...mapGetters(['toplist'])
  },
  created () {
    this._getMusicList()
  },
  methods: {
    _getMusicList () {
      if (!this.toplist.id) {
        this.$router.push({ name: 'Rank' })
        return false
      }
      getMusicList(this.toplist.id)
        .then(result => {
          // console.log(result)
          if (result.code === ERR_OK) {
            // this.songs = this._normalizeSongs(result.songlist)
            processSongsUrl(this._normalizeSongs(result.songlist))
              .then(response => {
                this.songs = response
              })
          }
        })
        .catch(error => {
          console.error(error)
        })
    },
    _normalizeSongs (list) {
      const array = []
      list.forEach((item, index) => {
        const { data } = item
        if (data.songid && data.albummid) {
          if (isValidMusic(data)) {
            array.push(createSong(data))
          }
          // getSongVkey(data.songmid)
          //   .then((result) => {
          //     // console.log(result)
          //     if (result.code === ERR_OK) {
          //       array.push((createSong(data, result.data.items[0].vkey)))
          //     }
          //   })
          //   .catch((error) => {
          //     console.error(error)
          //   })
        }
      })

      return array
    }
  },
  components: { MusicList }
}
</script>

<style lang="stylus" scoped rel="stylesheet/stylus">
@import '~common/stylus/variable';

.top-list {
  position: fixed;
  z-index: 100;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: $color-background;
}
</style>
